#include <ShareMemory/allocator.hpp>
using namespace demo::debug::log;
using namespace multiprocess::shm_allocator;

struct person {
  int age = 10;
  int name = 10;
};  // 8bytes

int main() {
  info("shm allocator test");
  ShareMemoryPool shm_pool(123, 20, 4);  // 40 - 4 - 1 bytes
  shm_pool.debug_str();

  info("shm_pool alloc {} bytes", sizeof(person));
  char* p = (char*)shm_pool.alloc(sizeof(person));

  info("shm_pool data read");
  ((person*)p)->age = 10;
  ((person*)p)->name = 10;
  shm_pool.debug_str();
  auto per = (person*)shm_pool.data();
  std::cout << per->age << " " << per->name << "\n";

  info("allocator test");
  ShareMemoryAllocator<int> int_allocator(&shm_pool);
  shm_pool.debug_str();

  info("allocator alloc 3 int");
  int* int_ptr = int_allocator.allocate(3);
  shm_pool.debug_str();

  ShareMemoryAllocator<char> char_alloc(&shm_pool);
  info("allocator alloc 2 char");
  char_alloc.allocate(2);
  shm_pool.debug_str();

  info("allocator dealloc 3 int");
  int_allocator.deallocate(int_ptr, 3);
  shm_pool.debug_str();

  return 0;
}
